import { useQueryClient } from '@umijs/max';
import { useMutation, useQuery } from '@umijs/max';
import { request } from '../axios';
import APP_PRE from '../constant/app-pre';
import QUERY_KEYS from '../constant/attention';

const prefix = `${APP_PRE.APP_NAME}/attention`;

const useAddAttention = (key: string) => {
  const queryCliet = useQueryClient();
  return useMutation(
    (attention: API.IAttention) =>
      request<API.IResult<API.IInsertId>>(`${prefix}/add`, {
        method: 'POST',
        data: attention,
      }),
    {
      onSuccess: (res: API.IResult<API.IInsertId>) => {
        if (res.code === 200) {
          queryCliet.invalidateQueries([key]);
        }
      },
    },
  );
};

const useDeleteAttention = (key: string) => {
  const queryCliet = useQueryClient();
  return useMutation(
    (id: number) =>
      request<API.IResult<API.IInsertId>>(`${prefix}/delete`, {
        method: 'POST',
        params: { attentionId: id },
      }),
    {
      onSuccess: (res: API.IResult<API.IInsertId>) => {
        if (res.code === 200) {
          queryCliet.invalidateQueries([key]);
        }
      },
    },
  );
};

const useGetAllAttention = (accountId: number) => {
  return useQuery([QUERY_KEYS.ATTENTION_GET_ALL], () =>
    request<API.IResult<API.IUserInfo[]>>(`${prefix}/getAll`, {
      method: 'GET',
      params: {
        accountId,
      },
    }),
  );
};

export { useAddAttention, useDeleteAttention, useGetAllAttention };
